using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Object = UnityEngine.Object;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelBuffOverView : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelBuffOverView
        {
            #region ObjectBinding Generate

            public GameObject commonBuffUI { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<GameObject>("commonBuffUI", out var __tbv0);
                commonBuffUI = __tbv0;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        protected UIB_UIN_LevelBuffOverView ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #region Override

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelBuffOverView();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _commonBuffUI = ui.commonBuffUI;
            _commonBuffUI.SetActive(false);
            //buffItem 需要调用Update 因此需要把父节点的 isUpdate 打开
            isUpdate = true;
        }

        protected override void OnShow()
        {
            UpdateLevelBuffData();
            Refresh();
        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            foreach (var buffItem in _buffItems)
            {
                buffItem?.Dispose();
            }
        }

        protected override void AddMsgListeners()
        {
            //buff添加，参数：buffData，isNew
            currMsgDispatcher.AddListener<LevelBuffData, bool, LevelBuffHostType, long>(MsgEventType.Level_UpdateBuff, OnLevelBuffUpdate);
            //buff移除，参数：buffData
            currMsgDispatcher.AddListener<LevelBuffData, LevelBuffHostType, long>(MsgEventType.Level_RemoveBuff, OnLevelBuffRemove);
        }
        #endregion

        #region Method

        private UIN_LevelBuffItem CreateBuffItem()
        {
            var go = _buffItems.First() == null
                ? _commonBuffUI
                : Object.Instantiate(_commonBuffUI, _commonBuffUI.transform.parent);
            return InitChildNode<UIN_LevelBuffItem>(go);
        }

        /// <summary>
        /// 核心逻辑 取buff数据 最多5个
        /// </summary>
        private void UpdateLevelBuffData()
        {
            _levelBuff.Clear();
            var buffDataCollection = LevelPlayUtil.GetDynLevelPlayer().buffs;
            var count = buffDataCollection.Count >= MaxShowCount ? MaxShowCount : buffDataCollection.Count;
            var index = 0;
            foreach (var buffData in buffDataCollection)
            {
                _levelBuff.Add(buffData);
                index++;
                if (index == count)
                {
                    break;
                }
            }
        }

        private void Refresh()
        {
            if (_levelBuff.Count == 0)
            {
                SetActive(false);
                return;
            }
            SetActive(true);

            for (var i = 0; i < _levelBuff.Count; i++)
            {
                _buffItems[i] ??= CreateBuffItem();
                _buffItems[i].ShowByData(_levelBuff[i]);
            }

            for (var i = _levelBuff.Count; i < MaxShowCount; i++)
            {
                _buffItems[i]?.Hide();
            }
        }

        private void OnLevelBuffUpdate(LevelBuffData buffData, bool isNew, LevelBuffHostType hostType, long hostId)
        {
            UpdateLevelBuffData();
            Refresh();
        }

        private void OnLevelBuffRemove(LevelBuffData buffData, LevelBuffHostType hostType, long hostId)
        {
            UpdateLevelBuffData();
            Refresh();
        }

        #endregion

        #region field

        private const int MaxShowCount = 5;
        private readonly List<LevelBuffData> _levelBuff = new();

        private GameObject _commonBuffUI;
        private readonly UIN_LevelBuffItem[] _buffItems = new UIN_LevelBuffItem[MaxShowCount];

        #endregion
    }
}
